package bh

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProviders
import yyee.llee.mmee.base_api_net.base_api_bean.bean.CalendarBean
import yyee.llee.mmee.base_api_net.base_api_bean.bean.CityBean
import yyee.llee.mmee.base_api_net.base_api_bean.bean.FutureInfo
import yyee.llee.mmee.base_api_net.base_api_bean.bean.GuideToLifeBean
import yyee.llee.mmee.base_api_net.base_api_bean.bean.ToadyWeatherBean
import yyee.llee.mmee.common.databinding.FragmentWeatherChildBinding
import yyee.llee.mmee.viewmode.WeatherChildViewModel
import yyee.llee.mmee.viewmode.WeatherViewModel
import bh.GA.Companion.PARAM_CITY
import yyee.llee.mmee.weather.adapter.Forecast24hAdapter
import yyee.llee.mmee.weather.adapter.Forecast7dAdapter
import java.util.Calendar

class FZ :
    FR<FragmentWeatherChildBinding>() {

    private var TAG = "FZ"

    private lateinit var mCity: CityBean
    private var condCode: String? = null

    private val mViewModel by lazy {
        ViewModelProviders.of(this).get(WeatherChildViewModel::class.java)
    }
    private val viewModel by lazy {
        ViewModelProviders.of(requireActivity()).get(WeatherViewModel::class.java)
    }

    companion object {
        @JvmStatic
        fun newInstance(param: CityBean) =
            FZ().apply {
                arguments = Bundle().apply {
                    putSerializable(PARAM_CITY, param)
                }
            }
    }

    private val mForecastList by lazy { ArrayList<FutureInfo>() }
    private lateinit var mForecastAdapter7d: Forecast7dAdapter
    private lateinit var forecast24hAdapter: Forecast24hAdapter
    private var day: Int = 0

    override fun initView(view: FragmentWeatherChildBinding) {
        arguments?.let {
            mCity = it.getSerializable(PARAM_CITY) as CityBean
        }

        mForecastAdapter7d = Forecast7dAdapter(requireContext(), mForecastList)
        viewBinding!!.layoutForecast7d.rvForecast7.adapter = mForecastAdapter7d

        forecast24hAdapter = Forecast24hAdapter(requireContext())
        viewBinding!!.layoutForecast24h.rvForecastHour.adapter = forecast24hAdapter
    }

    override fun onStart() {
        super.onStart()

        Calendar.getInstance().apply {
            day = get(Calendar.DAY_OF_MONTH)
            viewBinding!!.tvDate.text =
                (get(Calendar.MONTH) + 1).toString() + "月" + day + "日 "
        }

    }

    override fun onResume() {
        super.onResume()
        condCode?.let {
            viewModel.setCondCode(it)
        }
        initAds(false)

    }

    override fun initListener() {
        viewBinding!!.tvWarning.setOnClickListener {
//            startActivity(Intent(activity, WeatherWarningActivity::class.java))
        }
    }

    override fun initData() {
        //请求实时 生活建议 24h天气数据
        mViewModel.loadData(mCity.areaCode)

        //获取万年历
        mViewModel.getCalendar()

        mViewModel.let { it ->
            it.weatherNow.observe(this) {
                showData(it)
            }
            it.goodArticle.observe(this) {

                viewBinding!!.layoutArticle.tvTitle.text = "——" + it[day - 1].title
                viewBinding!!.layoutArticle.tvContent.text = it[day - 1].content
            }
            it.weatherHour.observe(this) {
                if (it != null) {
                    viewBinding!!.layoutForecast24h.layout24hour.visibility = View.VISIBLE
                    forecast24hAdapter.setData(it.hourly_fcsts)
                } else {
                    viewBinding!!.layoutForecast24h.layout24hour.visibility = View.GONE
                }

            }
            it.suggestionBean.observe(this) { it ->
                showSuggestionData(it)
            }

            it.calendarBean.observe(this) {
                showCalendarData(it)
            }
            it.mExceptionLiveData.observe(this) {
//                "请检查当前网络设置".showToast()
            }
        }

        initAds(true)
    }

    private fun initAds(isFirst: Boolean) {
        if (!viewBinding!!.flAdv1.isVisible) {
//            addNativeView(AdConfig.IN_WEATHER_FLOW_ONE, viewBinding!!.flAdv1);
        }

        if (!viewBinding!!.flAdv2.isVisible) {
//            if (isFirst) {
//                Handler(Looper.getMainLooper()).postDelayed({
//                    addNativeView(AdConfig.IN_WEATHER_FLOW_TWO, viewBinding!!.flAdv2);
//                }, 1000)
//            } else {
//                addNativeView(AdConfig.IN_WEATHER_FLOW_TWO, viewBinding!!.flAdv2);
//            }
        }

        if (!viewBinding!!.flAdv3.isVisible) {
//            if (isFirst) {
//                Handler(Looper.getMainLooper()).postDelayed({
//                    addNativeView(AdConfig.IN_WEATHER_FLOW_THREE, viewBinding!!.flAdv3);
//                }, 2000)
//            } else {
//                addNativeView(AdConfig.IN_WEATHER_FLOW_THREE, viewBinding!!.flAdv3);
//            }
        }
    }


    private fun showForestD7ata(weatherNow: ToadyWeatherBean) {
        if (weatherNow != null) {
            viewBinding!!.layoutForecast7d.layout7day.visibility = View.VISIBLE
            mForecastList.clear()

            mForecastList.addAll(weatherNow.future)
            mForecastAdapter7d.notifyDataSetChanged()
//            mForecastAdapter7d?.setRange(min, max)
        } else {
            viewBinding!!.layoutForecast7d.layout7day.visibility = View.GONE
        }

    }


    //今明两天
    private fun showForest2Data(weatherNow: ToadyWeatherBean) {
        //今天
        viewBinding!!.layoutForecast2d.tvTodayStatus.text = weatherNow.realtime.info
        val temperatureToday = weatherNow.realtime.temperature + "°C"
        viewBinding!!.layoutForecast2d.tvTodayMaxAndMin.text = temperatureToday
        val todayCode = weatherNow.realtime.wid
        viewBinding!!.layoutForecast2d.ivTodayStatus.setImageResourceName(todayCode)

        viewBinding!!.layoutForecast2d.tvTomorrowStatus.text = weatherNow.future[0].weather
        val temperatureTom =
            weatherNow.future[0].temperature + "°C"
        viewBinding!!.layoutForecast2d.tvTomorrowMaxAndMin.text = temperatureTom
        val tomorrowCode = weatherNow.future[0].wid.day
        viewBinding!!.layoutForecast2d.ivTomorrowStatus.setImageResourceName(tomorrowCode)
    }

    //生活建议
    private fun showSuggestionData(it: GuideToLifeBean) {
        if (it != null) {
            viewBinding!!.tvUnit.visibility = View.VISIBLE
            viewBinding!!.layoutGuide.layoutGuide.visibility = View.VISIBLE
            viewBinding!!.layoutGuide.windBrief.text = it.life.kongtiao.v
            viewBinding!!.layoutGuide.humidityBrief.text = it.life.guomin.v
            viewBinding!!.layoutGuide.waterVolumeBrief.text = it.life.shushidu.v
            viewBinding!!.layoutGuide.bodyTemBrief.text = it.life.chuanyi.v
            viewBinding!!.layoutGuide.uvBrief.text = it.life.ziwaixian.v
            viewBinding!!.layoutGuide.dressingBrief.text = it.life.ganmao.v
            viewBinding!!.layoutGuide.exerciseBrief.text = it.life.diaoyu.v
            viewBinding!!.layoutGuide.carWashBrief.text = it.life.xiche.v
        } else {
            viewBinding!!.layoutGuide.layoutGuide.visibility = View.GONE
        }
    }

    //实时天气
    private fun showData(weatherNow: ToadyWeatherBean) {
        viewBinding!!.tvTemperature.text = weatherNow.realtime.temperature
        viewBinding!!.tvUnit.visibility = View.VISIBLE
        viewBinding!!.tvText.text = weatherNow.realtime.info
        viewBinding!!.ivIcon.setImageResourceName(weatherNow.realtime.wid)
        showForest2Data(weatherNow)
        showForestD7ata(weatherNow)

        var mCondCode = 0
        if (weatherNow.realtime.info.contains("晴")) {
            mCondCode = 0
        } else if (weatherNow.realtime.info.contains("阴") || weatherNow.realtime.info.contains("多云")) {
            mCondCode = 1
        } else if (weatherNow.realtime.info.contains("雷")) {
            mCondCode = 2
        } else if (weatherNow.realtime.info.contains("雨")) {
            mCondCode = 3
        } else if (weatherNow.realtime.info.contains("沙")) {
            mCondCode = 4
        } else if (weatherNow.realtime.info.contains("雾")) {
            mCondCode = 5
        }
        viewModel.setCondCode("" + mCondCode)

    }


    //万年历
    private fun showCalendarData(it: CalendarBean) {

        if (it != null) {
            viewBinding!!.layoutCalendar.layoutCalendar.visibility = View.VISIBLE
            val perCalendar = it.data
            viewBinding!!.layoutCalendar.tvLunarCalendar.text = perCalendar.lunar + "【农历】"
            viewBinding!!.layoutCalendar.tvLunarYear.text =
                perCalendar.lunarYear + " " + perCalendar.yearMonth + " " + perCalendar.weekday + "  |"
            viewBinding!!.layoutCalendar.tvDate.text = perCalendar.date.removeRange(IntRange(0, 4))
            viewBinding!!.layoutCalendar.tvWeek.text = FS.getWeekDay(date = perCalendar.date)
            viewBinding!!.layoutCalendar.tvFitting.text = perCalendar.suit
            viewBinding!!.layoutCalendar.tvTaboo.text = perCalendar.avoid
            viewBinding!!.layoutCalendar.tvSolarTerm.text = perCalendar.holiday
            viewBinding!!.layoutCalendar.tvPzTaboo.text = perCalendar.desc

        } else {
            viewBinding!!.layoutCalendar.layoutCalendar.visibility = View.GONE
        }
    }


}